import Eureka

//修改语言能力表单

class updateLanguageController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的外语能力
    var language:languageAbility = languageAbility()
    //传过来的resumeId
    var resumeId:String = String()
    
	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< PushRow<String>("languageType"){
                $0.title = "外语语种"
                $0.selectorTitle = "外语语种"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_LANGUAGE_TYPE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("languageMaster"){
                $0.title = "掌握程度"
                $0.selectorTitle = "掌握程度"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_LANGUAGE_MASTER)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("rwability"){
                $0.title = "读写能力"
                $0.selectorTitle = "读写能力"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_LANGUAGE_MASTER)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("lsability"){
                $0.title = "听说能力"
                $0.selectorTitle = "听说能力"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_LANGUAGE_MASTER)
                $0.options = array
                $0.value = array[0]
            }
            
            +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "保存"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })
        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "删除"
                row.cell.tintColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.delete()
                })

        loadData()
    }

    private func loadData(){

        if(language.LanguageId != ""){
            var pr:PushRow<String>

            pr=form.rowByTag("languageType")!
            pr.value=language.lantype
            pr=form.rowByTag("languageMaster")!
            pr.value=language.lanmaster
            pr=form.rowByTag("rwability")!
            pr.value=language.lanrwability
            pr=form.rowByTag("lsability")!
            pr.value=language.lanlsability
        }
        

    }

    private func submitForm(){
        // 等待提示
        waiting.show(self)
        //获取表单结果
        let dict = form.values()
        
        //获取resumeId，由segue传输过来
//        let resumeId = self.resumeId
        let languageType = enumUtil.getKeyByString(dict["languageType"] as! String,dict: enumUtil.RESUME_LANGUAGE_TYPE)
        let languageMaster = enumUtil.getKeyByString(dict["languageMaster"] as! String,dict: enumUtil.RESUME_LANGUAGE_MASTER)
       	let rwability = enumUtil.getKeyByString(dict["rwability"] as! String,dict: enumUtil.RESUME_LANGUAGE_MASTER)
       	let lsability = enumUtil.getKeyByString(dict["lsability"] as! String,dict: enumUtil.RESUME_LANGUAGE_MASTER)
        
        resumeManager.UpdateLanguageAbility(language.LanguageId,
                             languageType:languageType,
                             languageMaster:languageMaster,
                             rwability:rwability,
                             lsability:lsability,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }


    private func delete(){
        // 等待提示
        waiting.show(self)

        resumeManager.DeletLanguageAbility(language.LanguageId,
                             resumeId:resumeId,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


